package org.jeecg.modules.platformMasterEndPoint.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.RestUtil;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.jx.constant.FileConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.util.UUID;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @Author scott
 * @since 2018-12-20
 */
@Slf4j
@RestController
@RequestMapping("/jx/imageUpload")
public class JxImageUploadController {

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    /**
     * 本地：local minio：minio 阿里：alioss
     */
    @Value(value="${jeecg.uploadType}")
    private String uploadType;

    /**
     * @Author 政辉
     * @return
     */
    @GetMapping("/403")
    public Result<?> noauth()  {
        return Result.error("没有权限，请联系管理员授权");
    }

    /**
     * 文件上传统一方法
     * @param request
     * @return
     */
    @PostMapping(value = "/upload")
    public Result<?> upload(HttpServletRequest request) {
        String savePath = "";

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获取上传文件对象
        MultipartFile file = multipartRequest.getFile("file");
        String fileName = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
        savePath = this.uploadLocal(file, fileName);
        JSONObject resultData = new JSONObject();
        resultData.put("fileName", fileName);
        if(oConvertUtils.isNotEmpty(savePath)){
            return Result.ok(resultData);
        }else {
            return Result.error("上传失败！");
        }
    }

    /**
     * 本地文件上传
     * @param mf 文件
     * @return
     */
    private String uploadLocal(MultipartFile mf, String fileName){
        try {
            File uploadTempDir = new File(FileConstants.FILE_UPLOAD_TEMP_DIR);
            if (!uploadTempDir.exists()) {
                // 上传的文件，都先放在临时目录下，此处判断临时目录是否存在，若不存在，则先创建临时目录
                uploadTempDir.mkdirs();
            }

            String savePath = uploadTempDir.getPath() + File.separator + fileName;
            File savefile = new File(savePath);
            FileCopyUtils.copy(mf.getBytes(), savefile);

            return savefile.getName();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

}
